<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">体系阶段管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>体系阶段管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <input id="stage-sys-search-id" class="layui-input search-input" type="text" placeholder="输入体系id"/>
            <input id="stage-sys-search-value" class="layui-input search-input" type="text" placeholder="输入阶段名"/>&emsp;
            <button id="stage-sys-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="stage-sys-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            <!--            <button id="stage-sys-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>-->
        </div>
        <table class="layui-table" id="stage-system-table" lay-filter="stage-system-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="stage-sys-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="look">查看详情</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="stage-sys-tpl-state">
    <input type="checkbox" lay-filter="stage-sys-tpl-state" value="{{d.stcode}}" lay-skin="switch" lay-text="使用|禁用"
           {{d.status==1?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin','layer'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 添加按钮绑定权限
        /*if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-add').hide();
        }*/

        // 导出按钮绑定权限
        /*if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-export').hide();
        }*/

        // 渲染表格
        table.render({
            elem: '#stage-system-table',
            url: config.base_server + 'api-study/stage/getList',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'stcode', sort: true, title: '阶段编号', width: 110},
                {field: 'stitle', sort: true, title: '阶段名称'},
                {field: 'stades', title: '描述'},
                {field: 'stesti', sort: true, title: '预计停留时间（天）',width: 100},
                {field: 'sorder', sort: true, title: '排序',width: 50},
                {field: 'cretim', sort: true, title: '创建时间'},
                {field: 'status', sort: true, templet: '#stage-sys-tpl-state', title: '状态', width: 150},
                {align: 'center', toolbar: '#stage-sys-table-bar', title: '操作', width: 150}
            ]]
        });
        // 搜索按钮点击事件
        $('#stage-sys-btn-search').click(function () {
            var id = $('#stage-sys-search-id').val();
            var value = $('#stage-sys-search-value').val();
            table.reload('stage-system-table', {where: {tsysId:id,stitle: value}});
        });

        // 添加按钮点击事件
        $('#stage-sys-btn-add').click(function () {
            showEditModel();
        });


        table.on('tool(stage-system-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改

                showEditModel(data);
            } else if (layEvent === 'look') { // 查看详情

            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            // console.log("sds")
            admin.putTempData('t_stage_sys', data);
            var title = data ? '修改阶段' : '添加阶段';
            admin.popupCenter({
                title: title,
                path: 'pages/study/stage_form.html',
                finish: function () {
                    table.reload('stage-system-table');
                }
            });
        };

        // 修改状态
        form.on('switch(stage-sys-tpl-state)', function (obj) {
            layer.load(2);
            var data = new FormData();
            data.append("stcode", obj.elem.value);
            data.append("status", obj.elem.checked ? 1 : 2);
            $.ajax({
                url: config.base_server + 'api-study/stage/delete',
                data: data,
                type: "post",
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function (data) {
                    layer.closeAll('loading');
                    if (data.code === 0) {
                        layer.msg(data.msg, {icon: 1, time: 1000});
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 1000});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                    }
                },
                beforeSend: function (xhr) {
                    var token = config.getToken();
                    if (token) {
                        xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                    }
                }
            });
        })
        // 工具条点击事件
        /*

         // 导出
         $('#user-btn-export').click(function () {
             var url = config.base_server + 'api-user/users/exportUser?access_token='+config.getToken().access_token;
             var xhr = new XMLHttpRequest();
             xhr.open('POST', url, true);
             xhr.responseType = "blob";
             xhr.setRequestHeader("client_type", "DESKTOP_WEB");
             xhr.onload = function() {
                 if (this.status == 200) {
                     var blob = this.response;
                     var objecturl = URL.createObjectURL(blob);
                     window.location.href = objecturl;
                 }
             }
             xhr.send();
         });*/


    });
</script>